//
//  LGBodayStatisticsCell.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/26.
//

#import "LGBodayStatisticsWeightView.h"
#import "LGScrollView.h"
#define topMargin    viewPix(100)

#define leftMargin   viewPix(45)+iPadMargin(10)

#define lineHeight   viewPix(25)

#define scrollWidth   (Screen_W - viewPix(95))

#define scrollHeight  (lineHeight*11+viewPix(60))

@interface LGBodayStatisticsWeightView()<UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)LGEmptyView *emptyView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;
/**   */
@property (nonatomic , strong)LGScrollView *scrollView;
/**   */
@property (nonatomic , strong)NSMutableArray *dayArray;
/**   */
@property (nonatomic , strong)NSMutableArray *buttonArray;
/**   */
@property (nonatomic , strong)NSMutableArray *layerArray;
/**   */
@property (nonatomic , strong)UIButton *leftButton;
/**   */
@property (nonatomic , strong)UIButton *rightButton;
/**   */
@property (nonatomic , strong)NSArray *highArray;
/**   */
@property (nonatomic , strong)NSArray *lowArray;
/**   */
@property (nonatomic , assign)NSInteger totalCount;
/**   */
@property (nonatomic , assign)CGFloat itemWidth;
@end

@implementation LGBodayStatisticsWeightView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.itemWidth = scrollWidth/7.0;
        [self createSubView];
    }
    return self;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    self.emptyView.hidden = dataArray.count>0?YES:NO;
    self.totalCount = dataArray.count>0?dataArray.count:1;
    NSInteger count = (self.totalCount>5)?5:self.totalCount;
    self.itemWidth = scrollWidth/count;
    CGFloat totalWidth = viewPix(20)+self.itemWidth*self.totalCount;
    self.scrollView.contentSize = CGSizeMake(totalWidth, scrollHeight);
    [self.scrollView setContentOffset:CGPointMake(totalWidth-scrollWidth-viewPix(10), 0)];
    
    for (UILabel *label in self.dayArray) {
        [label removeFromSuperview];
    }
    
    for (UIButton *button in self.buttonArray) {
        [button removeFromSuperview];
    }
    
    for (CAShapeLayer *layer in self.layerArray) {
        [layer removeFromSuperlayer];
    }
    [self.dayArray removeAllObjects];
    [self.buttonArray removeAllObjects];
    [self.layerArray removeAllObjects];
    
    NSMutableArray *dayArray = [NSMutableArray array];
    NSMutableArray *pointArray = [NSMutableArray array];
    NSMutableArray *pointArray2 = [NSMutableArray array];
    for (NSInteger i=0; i<dataArray.count; i++){
        NSDictionary *tempDic = dataArray[i];
        //创建X轴
        NSString *dayString = [LGNSString(tempDic[@"markDate"]) stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
        UILabel *dayLabel = [UILabel lableWithText:dayString colorString:@"#928F94" textFont:LGFont(11) textAlignment:NSTextAlignmentCenter lines:1];
        dayLabel.frame = CGRectMake(viewPix(5)+self.itemWidth*i, lineHeight*11+viewPix(3), self.itemWidth+viewPix(5), lineHeight);
        dayLabel.transform = CGAffineTransformRotate(dayLabel.transform, -M_PI_4);
        [self.scrollView addSubview:dayLabel];
        [dayArray addObject:dayLabel];
        
        //创建点
        CGFloat pointY1 = scrollHeight-(([tempDic[@"recNum"] floatValue]-0)/20.0*lineHeight+viewPix(60)+lineHeight*0.5);
        UIButton *button1 = [UIButton buttonWithImage:@"体征数据-紫色" selectImage:@"体征数据-紫色"];
        button1.bounds = CGRectMake(0, 0, viewPix(20), viewPix(20));
        button1.center = CGPointMake(viewPix(10)+self.itemWidth/2.0+self.itemWidth*i, pointY1);
        button1.tag = 1000+i;
        [button1 addTarget:self action:@selector(pointButtonTouch:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button1];
        [self.buttonArray addObject:button1];
        NSValue *point1 = [NSValue valueWithCGPoint:button1.center];
        [pointArray addObject:point1];
        
        CGFloat pointY2 = scrollHeight-(([tempDic[@"remark"] floatValue]-0)/20.0*lineHeight+viewPix(60)+lineHeight*0.5);
        UIButton *button2 = [UIButton buttonWithImage:@"体征数据-蓝色" selectImage:@"体征数据-蓝色"];
        button2.bounds = CGRectMake(0, 0, viewPix(20), viewPix(20));
        button2.center = CGPointMake(viewPix(10)+self.itemWidth/2.0+self.itemWidth*i, pointY2);
        button2.tag = 2000+i;
        [button2 addTarget:self action:@selector(pointButtonTouch:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button2];
        [self.buttonArray addObject:button2];
        NSValue *point2 = [NSValue valueWithCGPoint:button2.center];
        [pointArray2 addObject:point2];
    }
    [self drawCurveLine:pointArray lineColor:@"#796DF5"];
    [self drawCurveLine:pointArray2 lineColor:@"#198EFD"];
    self.dayArray = dayArray;
}

-(void)pointButtonTouch:(UIButton *)sender{
    NSInteger index = sender.tag%1000;
    for(NSInteger i=0; i<self.totalCount; i++){
        UIButton *button = [self viewWithTag:1000+i];
        button.selected = (index == i)?YES:NO;
    }
    NSDictionary *leftDic = @{@"title":@"体重(kg)",@"score":LGNSString(self.dataArray[index][@"recNum"]),@"status":@""};
    NSDictionary *rightDic = @{@"title":@"BMI",@"score":LGNSString(self.dataArray[index][@"remark"]),@"status":@""};
    NSArray *tempArray = @[leftDic,rightDic];
    CGPoint point = [self.scrollView convertPoint:sender.center toView:self.superview];
    NSLog(@">>>???>>>%f--%f",point.x,point.y);
    if(self.delegate && [self.delegate respondsToSelector:@selector(showAlertView:date:point:)]){
        [self.delegate showAlertView:tempArray date:LGNSString(self.dataArray[index][@"markDate"]) point:point];
    }
}


//画线
-(void)drawCurveLine:(NSArray *)arry lineColor:(NSString *)color{
    if(arry.count == 0) return;
    CGPoint startPoint = [[arry objectAtIndex:0] CGPointValue];
    CGPoint lastPoint;
    UIBezierPath *lineBeizer = [UIBezierPath bezierPath];
    [lineBeizer setLineWidth:2.0];
    [lineBeizer moveToPoint:startPoint];

    for (NSInteger i = 0;i < arry.count;i++ ) {
        if (i <arry.count-1 ) {
            CGPoint nowPoint = [[arry objectAtIndex:i] CGPointValue];
            CGPoint futPoint  = [[arry objectAtIndex:i+1] CGPointValue];
            [lineBeizer addCurveToPoint:futPoint controlPoint1:CGPointMake((nowPoint.x+futPoint.x)/2, nowPoint.y) controlPoint2:CGPointMake((nowPoint.x+futPoint.x)/2, futPoint.y)];
            if (i == arry.count-2) {
                [lineBeizer moveToPoint:futPoint];//添加连线
                lastPoint = futPoint;
            }
        }
    }
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = lineBeizer.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor colorWithString:color].CGColor;
    shapeLayer.lineWidth = 2.0;
    [self.scrollView.layer addSublayer:shapeLayer];
    [self.layerArray addObject:shapeLayer];
    for(NSInteger i=0; i<self.totalCount; i++){
        UIButton *button = [self viewWithTag:1000+i];
        UIButton *button2 = [self viewWithTag:2000+i];
        [self.scrollView bringSubviewToFront:button];
        [self.scrollView bringSubviewToFront:button2];
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(chartViewTouchesBegan)]){
        [self.delegate chartViewTouchesBegan];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if(self.delegate && [self.delegate respondsToSelector:@selector(chartViewTouchesBegan)]){
        [self.delegate chartViewTouchesBegan];
    }
}


-(void)createSubView{
    self.backgroundColor = [UIColor whiteColor];
    self.cornerRidus = 20;
    [self addSubview:self.titleLabel];
    [self addSubview:self.dayButton7];
    [self addSubview:self.dayButton30];
    [self addSubview:self.scrollView];
    [self addSubview:self.unitLabel];
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];
    NSArray *titleArray = @[@"200",@"180",@"160",@"140",@"120",@"100",@"80",@"60",@"40",@"20",@"0"];
    for (NSInteger i=0; i<titleArray.count; i++) {
        //Y轴标题
        UILabel *label = [UILabel lableWithText:titleArray[i] colorString:@"#333333" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        label.frame = CGRectMake(viewPix(5), topMargin+lineHeight*i, leftMargin-5, lineHeight);
        [self addSubview:label];
        //虚线
        LGDottedLineView *lineView = [[LGDottedLineView alloc]initWithFrame:CGRectMake(0, lineHeight*i, self.itemWidth*30+viewPix(50), lineHeight)];
        [self.scrollView addSubview:lineView];
    }
    [self addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(viewPix(80));
        make.left.bottom.right.equalTo(self);
    }];
}


-(LGEmptyView *)emptyView{
    if(!_emptyView){
        _emptyView = [[LGEmptyView alloc]init];
        _emptyView.backgroundColor = [UIColor whiteColor];
        _emptyView.imageView.image = [UIImage imageNamed:@"emptyView2"];
        _emptyView.contentLabel.text = @"暂无数据";
        _emptyView.offset = viewPix(30);
        _emptyView.needBuffer = NO;
        _emptyView.hidden = YES;
    }
    return _emptyView;
}
-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"体重" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), viewPix(200), viewPix(23));
    }
    return _titleLabel;
}

-(UIButton *)dayButton7{
    if(!_dayButton7){
        _dayButton7 = [UIButton buttonWithBackColor:@"#EFEFEF" cornerRadius:viewPix(13) title:@"7天" titleFont:LGFont(12) textColor:@"#999999" frame:CGRectMake(viewPix(16), viewPix(50), viewPix(45), viewPix(26))];
        _dayButton7.selected = YES;
        [_dayButton7 setSelectTextColor:@"#796DF5" backColor:@"#EFEAFF"];
    }
    return _dayButton7;
}

-(UIButton *)dayButton30{
    if(!_dayButton30){
        _dayButton30 = [UIButton buttonWithBackColor:@"#EFEFEF" cornerRadius:viewPix(13) title:@"30天" titleFont:LGFont(12) textColor:@"#999999" frame:CGRectMake(kMaxX(self.dayButton7.frame)+viewPix(10), viewPix(50), viewPix(45), viewPix(26))];
        [_dayButton30 setSelectTextColor:@"#796DF5" backColor:@"#EFEAFF"];
    }
    return _dayButton30;
}


-(LGScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[LGScrollView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, scrollWidth, scrollHeight)];
        _scrollView.contentSize = CGSizeMake(self.itemWidth*30+viewPix(20), scrollHeight);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"kg" colorString:@"#928F94" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        _unitLabel.frame = CGRectMake(8, topMargin-10, leftMargin-5, viewPix(17));
    }
    return _unitLabel;
}

-(UIButton *)leftButton{
    if(!_leftButton){
        _leftButton = [UIButton buttonWithTitle:@" 体重（kg）" titleFont:LGFont(15) textColor:@"#333333" imageName:@"体征数据-紫色"];
        _leftButton.frame = CGRectMake(0, kMaxY(self.scrollView.frame)-viewPix(10), (Screen_W-viewPix(32))/2.0-viewPix(10), viewPix(20));
        _leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }
    return _leftButton;
}

-(UIButton *)rightButton{
    if(!_rightButton){
        CGFloat margin = (Screen_W-viewPix(64)-viewPix(122)*2-viewPix(45)-iPadMargin(50)*3)/2.0;
        _rightButton = [UIButton buttonWithTitle:@" BMI" titleFont:LGFont(15) textColor:@"#333333" imageName:@"体征数据-蓝色"];
        _rightButton.frame = CGRectMake(kMaxX(self.leftButton.frame)+viewPix(20), kMinY(self.leftButton.frame), (Screen_W-viewPix(32))/2.0-viewPix(10), viewPix(20));
        _rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
    return _rightButton;
}

-(NSMutableArray *)dayArray{
    if(!_dayArray){
        _dayArray = [NSMutableArray array];
    }
    return _dayArray;
}

-(NSMutableArray *)buttonArray{
    if(!_buttonArray){
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

-(NSMutableArray *)layerArray{
    if(!_layerArray){
        _layerArray = [NSMutableArray array];
    }
    return _layerArray;
}

@end
